﻿/*----------------------------------------------------------------
    // Copyright (C) 2009 江苏华招网
    // 版权所有。 
    // 作者：吕海乐 
    //
    // 文件名：BidProductList.cs
    // 文件功能描述：采购产品管理：采购产品列表
    // 创建标识：吕海乐2009-03-19
    //
    // 修改标识：
    // 修改描述：
    //
    // 修改标识：
    // 修改描述：
    //
    // 审核人：
// 审核时间：
// 审核说明：
 //----------------------------------------------------------------*/
using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;

using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Collections.Generic;
using HZ.DrugTrade.Web;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.Model.Enum;

namespace HZ.DrugTrade.Web.Admin
{
    public partial class BidProductList : BasePage
    {
        IBLL_ProcurementBasic pbCtrl = Factory.BLL_ProcurementBasic();
        IBLL_ProcurementBasic ipb = Factory.BLL_ProcurementBasic();
        IBLL_Sort isort = Factory.IBLL_Sort();
        string flag = "";
        bool bCatalogState = false;
        string strAreaID = "";
        int RealPrice = 10;//实际采购价
        int AreaPrice = 16;//查看地区采购价
        int ModifyPrice = 17;//采购价调整

        public BidProductList()
        {
            this.ModuleNo = Module.采购产品管理.ToString();
            this.PageOperateType = OperateType.采购产品列表.ToString();
        }
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                //绑定所有项目
                if (Request.QueryString["flag"] != null)
                {
                 
                    flag = Request.QueryString["flag"].ToString();
                }
                if (Request.QueryString["name"] != null)
                {
                    this.txtGoodsName.Text = Request.QueryString["name"].ToString();
                }
                if (Request.QueryString["ComSc"] != null)
                {
                    this.txtProduceCompany_SC.Text = Request.QueryString["ComSc"].ToString();
                }

                //ProcurementProject procurementProject = new ProcurementProject();
                //procurementProject.ProcureStartTimeFrom = Convert.ToDateTime("1890-01-01");
                //procurementProject.ProcureStartTimeTo = Convert.ToDateTime("2099-12-31");
                //procurementProject.ProcureEndTimeFrom = Convert.ToDateTime("2000-01-01");
                //procurementProject.ProcureEndTimeTo = Convert.ToDateTime("2099-12-31");
                //int temp;
                //IList<ProcurementProject> projectList = ipb.GetProjectByWhere(procurementProject, 1, 0, out temp);
                //ddlProject.DataSource = projectList;
                //ddlProject.DataTextField = "ProjectName";
                //ddlProject.DataValueField = "ProjectID";
                //ddlProject.DataBind();
                //ListItem item = new ListItem();
                //item.Text = Constants.ALL_ITEM_TEXT;
                //item.Value = Constants.ALL_ITEM_VALUE;
                //ddlProject.Items.Insert(0, item);

                //绑定分类名
                IList<string> sort = isort.GetSortName();
                ddlSortName.DataSource = sort;
                ddlSortName.DataBind();
               

                if (!string.IsNullOrEmpty(Request.QueryString["ProjectId"]))
                {
                    for (int i = 0; i < ddlProjectid.Items.Count; i++)
                    {
                        if (ddlProjectid.Items[i].Value == Request.QueryString["ProjectId"]) { ddlProjectid.Items[i].Selected = true; }
                    }
                }
                //控件显示控制
                if (!CurrentAccount.GetUser().Org.Area.AreaID.Contains("0000"))
                {
                    this.btnAddBid.Visible = false;
                    this.btnAdd.Visible = false;
                    this.btnBatchAdd.Visible = false;
                    this.btnAbandon.Visible = false;
                    this.btnRecover.Visible = false;
                }

                //绑定列表
                setBind();
            }
        }


        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            setBind();
        }

        /// <summary>
        /// 翻页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
        {
            setBind();
        }

        private void setBind()
        {
            //获取查询条件
            Model.Query.ProcureCatalog procureCatalog = new HZ.DrugTrade.Model.Query.ProcureCatalog();
            procureCatalog.CompanyName_SC = txtProduceCompany_SC.Text.Trim();
            //procureCatalog.PinyinCode = this.txtPinyinCode.Text.Trim().ToUpper();
            procureCatalog.Material = txtMaterial.Text.Trim();
            procureCatalog.Outlookc = txtOutLook.Text.Trim();
            procureCatalog.PinyinCode = this.txtpinyincode.Text.Trim().ToUpper();
            procureCatalog.ProjectID = this.ddlProjectid.SelectedValue;
            procureCatalog.GoodsName = txtGoodsName.Text.Trim();
            procureCatalog.Unit = txtUnit.Text.Trim();
            //if (ucSource1.SelectValue != -1) procureCatalog.SortSource = ucSource1.SelectValue.ToString();
            //if (ucIsBasicRemedy1.SelectValue != -1) procureCatalog.IsBasicRemedy = ucIsBasicRemedy1.SelectValue;
            if (string.IsNullOrEmpty(HZ.Fun2.CheckString(this.txtGoodsID.Text.Trim())) == false
                && HZ.MathUtil.IsInt(this.txtGoodsID.Text.Trim()))
            {
                procureCatalog.GoodsID = this.txtGoodsID.Text.Trim();
            }

            //if (HZ.Common.MyValidate.IsNumeric(txtFactor.Text.Trim()))
            //{
            //    procureCatalog.Factor = Convert.ToInt32(txtFactor.Text.Trim());
            //}
            procureCatalog.CompanyName_TB = txtCompany_TB.Text;
            //procureCatalog.SortName = txtProductType.Text;

            //状态查询
            switch (ddlStatus.SelectedValue)
            {
                case "0":
                    break;
                case "1":
                    procureCatalog.IsUsing = true;
                    break;
                case "2":
                    procureCatalog.IsUsing = false;
                    break;
            }
            //分类名查询
            if (ddlSortName.SelectedIndex!=0)
            {
                procureCatalog.SortName = ddlSortName.SelectedItem.ToString();
            }

            
          
            procureCatalog.SortFields = " GoodsName asc,goodsid";

            if (!string.IsNullOrEmpty(txtAddTimeFrom.Text.Trim()))
            {
                procureCatalog.AddTimeFrom = Convert.ToDateTime(txtAddTimeFrom.Text.Trim());
            }
            if (!string.IsNullOrEmpty(txtAddTimeTo.Text.Trim()))
            {
                procureCatalog.AddTimeTo = Convert.ToDateTime(txtAddTimeTo.Text.Trim());
            }

            //if (ucIsBasicRemedy1.SelectValue != -1) procureCatalog.IsBasicRemedy = ucIsBasicRemedy1.SelectValue;
            //if (!string.IsNullOrEmpty(this.ddlNMJY.SelectedValue))//是否内蒙基药查询条件 add by zdc
            //{
            //    procureCatalog.IsNMJBYW = Convert.ToInt32(this.ddlNMJY.SelectedValue);
            //}

            if (CurrentAccount.GetUser().UserID != "ROOT" &&  CurrentAccount.GetUser().UserRole.RoleID == "03")
            {
                IBLL_ProcurementBasic ibb = Factory.BLL_ProcurementBasic();
                //读取TB_ManageOrg表查看 权限
                DataTable dt = ibb.GetTB_ManageOrg(CurrentAccount.GetUser().UserID);
                if (dt.Rows.Count > 0)
                {
                    strAreaID = dt.Rows[0]["AreaID"].ToString();
 
                    if (dt.Rows[0]["CatalogState"].ToString() == "1")
                    {
                        bCatalogState = true;  //可修改权限
                    }

                    string CatalogType = dt.Rows[0]["CatalogType"].ToString();
                    if(CatalogType.Contains("0") && CatalogType.Contains("1"))
                    {
                        //有基药和非基药权限
                    }
                    //else if (CatalogType.Contains("0"))
                    //{
                    //    //只有基药权限 查询条件 加入
                    //    procureCatalog.IsBasicRemedy = 0;
                    //}
                    //else
                    //{ 
                    //    //只有非基药权限
                    //    procureCatalog.IsBasicRemedy = 1;
                    //}
                    //public bool BindCatalogType(object obj, string p)
                    //{
                    //    string[] str = obj.ToString().Split('|');
                    //    for (int i = 0; i < str.Length; i++)
                    //    {
                    //        if (str[i] == p) return true;
                    //    }
                    //    return false;
                    //}

                }
            }
            if(CurrentAccount.GetUser().UserID.Substring(0,1).ToUpper() == "A")
            {
                procureCatalog.IsUsing = true;
            }

            //调用方法，执行查询，分页显示产品信息
            //AspNetPager1.PageSize = Constants.GRIDVIEW_PAGE_SIZE;
            int totalCount = 0;
            IBLL_ProcurementBasic ipb = Factory.BLL_ProcurementBasic();
            try
            {
               //如果是root管理员 则读取视图 
                if (CurrentAccount.GetUser().Org.OrgID == "1" && CurrentAccount.GetUser().UserRole.RoleID == "03")
                {
                    IList<Model.View.ProcureCatalog> procureCatalogList = ipb.GetProcureCatalogByWhere(procureCatalog, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalCount);
                    AspNetPager1.RecordCount = totalCount;
                    gvwProduceCatalog.DataSource = procureCatalogList;
                    gvwProduceCatalog.DataBind();

                    gvwProduceCatalog.Columns[RealPrice].Visible = false; //实际采购价
                    gvwProduceCatalog.Columns[AreaPrice].Visible = true;  //查看地区采购价
                    gvwProduceCatalog.Columns[ModifyPrice].Visible = false; //采购价调整

                    
                }
                else 
                {
                    if (CurrentAccount.GetUser().UserRole.RoleID == "03")
                    //其他盟市管理员 把区域ID 做条件带过去
                    {
                        IList<Model.View.ProcureCatalog> procureCatalogList = ipb.GetProcureCatalogByWhereArea(strAreaID, procureCatalog, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalCount);
                        AspNetPager1.RecordCount = totalCount;
                        gvwProduceCatalog.DataSource = procureCatalogList;
                        gvwProduceCatalog.DataBind();

                        gvwProduceCatalog.Columns[RealPrice].Visible = true;
                        gvwProduceCatalog.Columns[AreaPrice].Visible = false;
                        gvwProduceCatalog.Columns[15].Visible = false;
                        gvwProduceCatalog.Columns[17].Visible = false;
                        gvwProduceCatalog.Columns[18].Visible = false;
                        gvwProduceCatalog.Columns[19].Visible = false; 
                        if (bCatalogState)
                        {
                            gvwProduceCatalog.Columns[ModifyPrice].Visible = false;
                        }
                    }
                }


                if (string.IsNullOrEmpty(flag))
                {
                    this.gvwProduceCatalog.Columns[19].Visible = true;
                }

            }
            catch (Exception ex)
            {
                PageError(ex.Message, "");
            }
        }

        /// <summary>
        /// 撤销
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAbandon_Click(object sender, EventArgs e)
        {
            string ids = Request["checkbox1"];

            if (!string.IsNullOrEmpty(ids))
            {
                string[] arraryIds = ids.Split(',');
                foreach (string Item in arraryIds)
                {
                    Model.View.ProcureCatalog tmpProcureCatalog = pbCtrl.GetProcureCatalogByID(Item);
                    if (Convert.ToBoolean(tmpProcureCatalog.IsUsing))
                    {
                        ProcureCatalog aProcureCatalog = new ProcureCatalog();
                        aProcureCatalog.IsUsing = false;
                        aProcureCatalog.ProcureCatalogID = tmpProcureCatalog.ProcureCatalogID;
                        int nSuccess = pbCtrl.UpdateProcureCatalog_IsUsing(aProcureCatalog);
                        if (nSuccess>0)
                        {
                            View_Procurecatalog_TB vpt = new View_Procurecatalog_TB();
                            vpt.IsUsing = false;
                            vpt.GoodsID = tmpProcureCatalog.ProcureCatalogID;
                            int success = pbCtrl.UpdateProcureCatalog_IsUsingZDC(vpt);
                        }
                        if (nSuccess == 0)
                        {
                            PageError("数据操作失败！", "");
                        }
                    }
                }
            }
            setBind();
        }

        /// <summary>
        /// 恢复
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnRecover_Click(object sender, EventArgs e)
        {
            string ids = Request["checkbox1"];

            if (!string.IsNullOrEmpty(ids))
            {
                string[] arraryIds = ids.Split(',');
                foreach (string Item in arraryIds)
                {
                    Model.View.ProcureCatalog tmpProcureCatalog = pbCtrl.GetProcureCatalogByID(Item);
                    if (!Convert.ToBoolean(tmpProcureCatalog.IsUsing))
                    {
                        ProcureCatalog aProcureCatalog = new ProcureCatalog();
                        aProcureCatalog.IsUsing = true;
                        aProcureCatalog.ProcureCatalogID = tmpProcureCatalog.ProcureCatalogID;
                        int nSuccess = pbCtrl.UpdateProcureCatalog_IsUsing(aProcureCatalog);
                        if (nSuccess > 0)
                        {
                            View_Procurecatalog_TB vpt = new View_Procurecatalog_TB();
                            vpt.IsUsing = true;
                            vpt.GoodsID = tmpProcureCatalog.ProcureCatalogID;
                            int success = pbCtrl.UpdateProcureCatalog_IsUsingZDC(vpt);
                        }
                        if (nSuccess == 0)
                        {
                            PageError("数据操作失败！", "");
                        }
                    }
                }
            }
            setBind();
        }

        /// <summary>
        /// 新增采购产品
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string projectID;
            if (!string.IsNullOrEmpty(Request.QueryString["ProjectId"]))
            {
                projectID = Request.QueryString["ProjectId"];
            }
            else
            {
                projectID = this.ddlProjectid.SelectedValue;
            }
            HZ.Web.WebUtil.RedirectByScript("BidProductAdd.aspx?projectID=" + projectID);
        }
        /// <summary>
        /// 新增中标产品
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAddBid_Click(object sender, EventArgs e)
        {
            HZ.Web.WebUtil.RedirectByScript("BidProductUAddNmg.aspx");
        }
        /// <summary>
        /// 简单查询与高级查询间的转换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbnSwitch_Click(object sender, EventArgs e)
        {
            //if (lbnSwitch.CommandArgument == "simple")
            //{
            //    ltlAdvance.Text = "";
            //    lbnSwitch.CommandArgument = "advance";
            //    lbnSwitch.Text = "简单查询";
            //}
            //else
            //{
            //    ltlAdvance.Text = "<tbody id='advance' style='display:none'>";
            //    lbnSwitch.CommandArgument = "simple";
            //    lbnSwitch.Text = "高级查询";

            //    //清空数据
            //    txtAddTimeFrom.Text = "";
            //    txtAddTimeTo.Text = "";
            //    txtCompany_TB.Text = "";
            //    txtFactor.Text = "";
            //    txtPinyinCode.Text = "";
            //    txtProduceCompany_SC.Text = "";
            //    txtProductType.Text = "";
            //    txtUnit.Text = "";
            //    txtGoodsName.Text = "";
            //    ddlProject.SelectedIndex = -1;
            //}
        }
        /// <summary>
        /// 有效品种导出2009-11-14
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnExport_Click(object sender, EventArgs e)
        {
            Model.Query.ProcureCatalog procureCatalog = new HZ.DrugTrade.Model.Query.ProcureCatalog();
            procureCatalog.CompanyName_SC = txtProduceCompany_SC.Text.Trim();
            //procureCatalog.PinyinCode = this.txtPinyinCode.Text.Trim().ToUpper();
            procureCatalog.Material = txtMaterial.Text.Trim();
            procureCatalog.Outlookc = txtOutLook.Text.Trim();
            procureCatalog.PinyinCode = this.txtpinyincode.Text.Trim().ToUpper();
            procureCatalog.ProjectID = this.ddlProjectid.SelectedValue;
            procureCatalog.GoodsName = txtGoodsName.Text.Trim();
            procureCatalog.Unit = txtUnit.Text.Trim();
            //if (ucSource1.SelectValue != -1) procureCatalog.SortSource = ucSource1.SelectValue.ToString();
            //if (ucIsBasicRemedy1.SelectValue != -1) procureCatalog.IsBasicRemedy = ucIsBasicRemedy1.SelectValue;
            if (string.IsNullOrEmpty(HZ.Fun2.CheckString(this.txtGoodsID.Text.Trim())) == false
                && HZ.MathUtil.IsInt(this.txtGoodsID.Text.Trim()))
            {
                procureCatalog.GoodsID = this.txtGoodsID.Text.Trim();
            }

            //if (HZ.Common.MyValidate.IsNumeric(txtFactor.Text.Trim()))
            //{
            //    procureCatalog.Factor = Convert.ToInt32(txtFactor.Text.Trim());
            //}
            procureCatalog.CompanyName_TB = txtCompany_TB.Text;
            //procureCatalog.SortName = txtProductType.Text;

            //状态查询
            switch (ddlStatus.SelectedValue)
            {
                case "0":
                    break;
                case "1":
                    procureCatalog.IsUsing = true;
                    break;
                case "2":
                    procureCatalog.IsUsing = false;
                    break;
            }
            //分类名查询
            if (ddlSortName.SelectedIndex != 0)
            {
                procureCatalog.SortName = ddlSortName.SelectedItem.ToString();
            }
            procureCatalog.SortFields = " GoodsName asc,goodsid";

            if (!string.IsNullOrEmpty(txtAddTimeFrom.Text.Trim()))
            {
                procureCatalog.AddTimeFrom = Convert.ToDateTime(txtAddTimeFrom.Text.Trim());
            }
            if (!string.IsNullOrEmpty(txtAddTimeTo.Text.Trim()))
            {
                procureCatalog.AddTimeTo = Convert.ToDateTime(txtAddTimeTo.Text.Trim());
            }
            if (CurrentAccount.GetUser().UserID != "ROOT" && CurrentAccount.GetUser().UserRole.RoleID == "03")
            {
                IBLL_ProcurementBasic ibb = Factory.BLL_ProcurementBasic();
                //读取TB_ManageOrg表查看 权限
                DataTable dt = ibb.GetTB_ManageOrg(CurrentAccount.GetUser().UserID);
                if (dt.Rows.Count > 0)
                {
                    strAreaID = dt.Rows[0]["AreaID"].ToString();

                    if (dt.Rows[0]["CatalogState"].ToString() == "1")
                    {
                        bCatalogState = true;  //可修改权限
                    }

                    string CatalogType = dt.Rows[0]["CatalogType"].ToString();
                    if (CatalogType.Contains("0") && CatalogType.Contains("1"))
                    {
                        //有基药和非基药权限
                    }

                }
            }
            if (CurrentAccount.GetUser().UserID.Substring(0, 1).ToUpper() == "A")
            {
                procureCatalog.IsUsing = true;
            }

            int totalcount = 0;
            IBLL_ProcurementBasic ipb = Factory.BLL_ProcurementBasic();
            DataTable dts = ipb.GetProcureCatalogDTByWhere(procureCatalog, 1, 0, out totalcount);
            //HZ.Data.ModelConvert.GetDataTableFromModel(procureCatalogList);
            
            HZ.Excel.ExcelHelper.ExportDataForWeb(dts, gvwProduceCatalog);
        }


       
    }
}
